import os
import logging
from common.handlepath import LOGDIR
from common.handleconfig import conf


class HandleLog(object):

    @staticmethod
    def create_logger():
        # 创建日志收集器对象
        mylog = logging.getLogger(conf.get('log', 'name'))
        mylog.setLevel(conf.get('log', 'level'))
        # 输出渠道（控制台），设置等级
        sh = logging.StreamHandler()
        sh.setLevel(conf.get('log', 'sh_level'))
        mylog.addHandler(sh)
        # 输出渠道(文件)，设置等级
        fh = logging.FileHandler(filename=os.path.join(LOGDIR, "log.log"), encoding="utf-8")
        fh.setLevel(conf.get('log', 'fh_level'))
        mylog.addHandler(fh)
        # 日志输出格式的设置
        formate = '%(asctime)s - [%(filename)s-->line:%(lineno)d] - %(levelname)s:%(message)s'
        fm = logging.Formatter(formate)
        sh.setFormatter(fm)
        fh.setFormatter(fm)

        return mylog


log = HandleLog.create_logger()
